package com.erclab.xbuilder.formhelpers;

import java.util.HashMap;

import com.erclab.internal.xpresso.ReportFactory;
import com.erclab.internal.xpresso.conf.Settings;
import com.erclab.internal.xpresso.dataAccess.MysqlDatasource;
import com.erclab.internal.xpresso.datavalidators.DataValidationError;
import com.erclab.internal.xpresso.exceptions.FormException;
import com.erclab.internal.xpresso.exceptions.ObjectCreatorException;
import com.erclab.internal.xpresso.exceptions.ReportException;
import com.erclab.internal.xpresso.forms.Form;
import com.erclab.internal.xpresso.forms.FormHelper;
import com.erclab.internal.xpresso.reports.Report;
import com.erclab.internal.xpresso.util.FormElementUtil;
import com.erclab.internal.xpresso.util.ObjectCreator;
import com.erclab.xbuilder.db.MysqlDatabaseManager;
import com.erclab.xbuilder.db.TableColumn;
import com.erclab.xbuilder.exception.DBManagerException;
import com.erclab.xbuilder.exception.XBuilderException;
import com.erclab.xbuilder.managers.MysqlFormManager;

public class ColumnCreatorHelper implements FormHelper {

	public void afterFormCreation(Form theForm) throws FormException {
		// Nada to do

	}

	public void afterModification(Form theForm, HashMap keyValues) throws FormException, DataValidationError {
		String action = theForm.getFormParameters().getAction();
		String destinationColumn = FormElementUtil.getElementValue(theForm, keyValues, "DestinationColumn");
		if (action.equals("insert")){
			String formName = getFormName(theForm);
			if (formName == null){
				throw new FormException("The parent form does not exist");
			}			
			String tableName = theForm.getFormParameters().getUser().getUserName() + "_" + formName;
			createTableColumn(destinationColumn, tableName);
			createReportColumn(destinationColumn, formName);
		}

	}

	protected void createReportColumn(String destinationColumn, String formName) throws FormException {
		ObjectCreator oc = Settings.getObjectCreator();
		try {
			Report r = ReportFactory.loadReport(formName);
			oc.createReportElement(destinationColumn, r.getIdReport() + "", 
					Settings.getRepository().getApplicationDataSource(
							Integer.parseInt(Settings.getProperty("xbuilder_app_meta.datasource"))));
		} catch (ReportException e) {
			throw new FormException("An error occurred retrieving report Id " + e);
		} catch (NumberFormatException e) {
			throw new FormException("An error occurred. Check the property: xbuilder_app_meta.datasource" + e);
		} catch (ObjectCreatorException e) {
			throw new FormException("An error occurred creating a report column" + e);
		}
		
	}

	protected String getFormName(Form theForm) throws FormException {
		try {
			return new MysqlFormManager().getFormName(theForm.getFormParameters().getUniqueKeyValue());
		} catch (XBuilderException e) {
			throw new FormException("An error occurred creating a table column: " + e.getTheException());
		}
	}

	private void createTableColumn(String destinationColumn, String tableName) throws FormException {
		
		MysqlDatabaseManager mdm = new MysqlDatabaseManager();
				
		try {
			TableColumn tc = new TableColumn(destinationColumn, "VARCHAR(100)");
			mdm.addColumnToTable(tableName, tc);
		} catch (DBManagerException e) {
			throw new FormException("An error occurred creating a table column: " + e.getTheException());
		} 
	}

	protected String getFormId(Form theForm) {
		if (theForm.getFormParameters().getAction().equals("insert")){
			return theForm.getFormParameters().getUniqueKeyValue();
		} else{
			return theForm.getFormParameters().getParentKeyValue();
		}
	}

	public void afterSubmission(Form theForm, HashMap keyValues)
			throws FormException, DataValidationError {
		// Nada to do

	}

	public void beforeFormCreation(Form theForm) throws FormException {
		// Nada to do

	}

}
